//
//  YTChangeViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/10/28.
//

#import "YTChangeViewController.h"

@interface YTChangeViewController ()<UITextFieldDelegate>
@property (nonatomic ,strong) UIView * phoneView;
@property (nonatomic ,strong) UITextField * phoneText;

@property (nonatomic ,strong) UIButton * delBtn;
@property (nonatomic ,strong) UIButton * delBtn1;
@property (nonatomic ,strong) UIButton * delBtn2;
@property (nonatomic ,strong) UIView * CodeView;
@property (nonatomic ,strong) UITextField * CodeText;
@property (nonatomic, nonatomic) UIButton *getCodeBtn;

@property (nonatomic ,strong) UIView * PasswordView;
@property (nonatomic ,strong) UITextField * PasswordText;

@property (nonatomic ,strong) UIView * PasswordView2;
@property (nonatomic ,strong) UITextField * PasswordText2;
@end

@implementation YTChangeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    
       MJWeakSelf
    UIView * backview = [UIView new];
    [self.view addSubview:backview];
    backview.backgroundColor = KcolorHex(@"#0967FF");
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    
    UIImageView * limg = kimageView(@"white_path");
    limg.contentMode = UIViewContentModeScaleAspectFit;
    limg.addClickBlock(^{
        [weakSelf back];
    });
    [backview addSubview:limg];
    [limg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(KTop/2);
        make.width.height.offset(kwt(20));
    }];
    
    UILabel * blab = [DTKTool labelWithText:@"修改密码" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:blab];
    limg.addClickBlock(^{
        [weakSelf back];
    });
    [blab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(42));
        make.centerY.equalTo(limg);
    }];
    
    UIView *phoneView = [UIView new];
    self.phoneView = phoneView;
    [self.view addSubview:phoneView];
    phoneView.backgroundColor =  KcolorHex(@"#F7F7F7");
    [DTKTool YJBK:kwt(5) andView:phoneView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backview.mas_bottom).offset(kwt(30));
        make.left.offset(kwt(16));
        make.right.offset(kwt(-16));
        make.height.offset(kwt(50));
    }];
    
    UIImageView *phoneImage = [UIImageView new];
    [phoneView addSubview:phoneImage];
    phoneImage.image = [UIImage imageNamed:@"account_number"];
    [phoneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(18));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));

    }];
        
    self.phoneText = [UITextField new];
    [phoneView addSubview:_phoneText];
    _phoneText.delegate = self;
    _phoneText.font = kFont(17);
    _phoneText.textColor = KcolorHex(@"#1E2425");
    _phoneText.placeholder = @"请输入原密码";
    _phoneText.secureTextEntry = YES;
    [_phoneText addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(42));
        make.centerY.offset(0);
        make.width.offset(kwt(290));
    }];
    
    self.delBtn = [UIButton new];
    self.delBtn.hidden = YES;
    [phoneView addSubview:_delBtn];
    [_delBtn setImage:[UIImage imageNamed:@"no_look"] forState:UIControlStateNormal];
    [_delBtn addTarget:self action:@selector(delClick) forControlEvents:UIControlEventTouchDown];
    [_delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-20));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));
    }];
    
   
    UIView *PasswordView = [UIView new];
    self.PasswordView = PasswordView;
    [self.view addSubview:PasswordView];
    PasswordView.backgroundColor =  KcolorHex(@"#F7F7F7");
    [DTKTool YJBK:kwt(5) andView:PasswordView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    [PasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneView.mas_bottom).offset(kwt(18));
        make.left.offset(kwt(16));
        make.right.offset(kwt(-16));
        make.height.offset(kwt(50));
    }];
    
    UIImageView *PasswordImage = [UIImageView new];
    [PasswordView addSubview:PasswordImage];
    PasswordImage.image = [UIImage imageNamed:@"password"];
    [PasswordImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(18));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));

    }];
        
    self.PasswordText = [UITextField new];
    [PasswordView addSubview:self.PasswordText];
    self.PasswordText.delegate = self;
    self.PasswordText.font = kFont(17);
    self.PasswordText.textColor = KcolorHex(@"#1E2425");
    self.PasswordText.placeholder = @"请设置新密码(6-20位数字、字母组成)";
    self.PasswordText.secureTextEntry = YES;
    [self.PasswordText addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];

    [self.PasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(42));
        make.centerY.offset(0);
        make.width.offset(kwt(300));
    }];
    
    self.delBtn1 = [UIButton new];
    self.delBtn1.hidden = YES;
    [PasswordView addSubview:_delBtn1];
    [_delBtn1 setImage:[UIImage imageNamed:@"no_look"] forState:UIControlStateNormal];
    [_delBtn1 addTarget:self action:@selector(delClick1) forControlEvents:UIControlEventTouchDown];
    [_delBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-20));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));
    }];
    
    UIView *PasswordView2 = [UIView new];
    self.PasswordView2 = PasswordView2;
    [self.view addSubview:PasswordView2];
    PasswordView2.backgroundColor =  KcolorHex(@"#F7F7F7");
    [DTKTool YJBK:kwt(5) andView:PasswordView2 andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    [PasswordView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(PasswordView.mas_bottom).offset(kwt(18));
        make.left.offset(kwt(16));
        make.right.offset(kwt(-16));
        make.height.offset(kwt(50));
    }];
    
  
    UIImageView *PasswordImage2 = [UIImageView new];
    [PasswordView2 addSubview:PasswordImage2];
    PasswordImage2.image = [UIImage imageNamed:@"password"];
    [PasswordImage2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(18));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));

    }];
        
    self.PasswordText2 = [UITextField new];
    [PasswordView2 addSubview:self.PasswordText2];
    self.PasswordText2.delegate = self;
    self.PasswordText2.font = kFont(17);
    self.PasswordText2.textColor = KcolorHex(@"#1E2425");
    self.PasswordText2.placeholder = @"请再次确认新密码";
    self.PasswordText2.secureTextEntry = YES;
    [self.PasswordText2 addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];

    [self.PasswordText2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(42));
        make.centerY.offset(0);
        make.width.offset(kwt(290));
    }];
    self.delBtn2 = [UIButton new];
    self.delBtn2.hidden = YES;
    [PasswordView2 addSubview:_delBtn2];
    [_delBtn2 setImage:[UIImage imageNamed:@"no_look"] forState:UIControlStateNormal];
    [_delBtn2 addTarget:self action:@selector(delClick2) forControlEvents:UIControlEventTouchDown];
    [_delBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-20));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));
    }];
    UIButton *loginBtn = [UIButton new];
    [self.view addSubview:loginBtn];
    loginBtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(345), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")];
    [loginBtn setTitle:@"完成" forState:UIControlStateNormal];
    loginBtn.titleLabel.font = kCFont(17);
    loginBtn.layer.masksToBounds = YES;
    loginBtn.layer.cornerRadius = kwt(5);
    [loginBtn addTarget:self action:@selector(OKClick) forControlEvents:UIControlEventTouchDown];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PasswordView2.mas_bottom).offset(kwt(24));
        make.width.offset(kwt(345));
        make.centerX.offset(0);
        make.height.offset(kwt(50));
    }];
    
}

- (void)textFieldChange:(UITextField *)textField {
    if(self.phoneText == textField){
        self.delBtn.hidden = (self.phoneText.text.length == 0);

    } else if(self.PasswordText == textField){
        self.delBtn1.hidden = (self.PasswordText.text.length == 0);

    } else if(self.PasswordText2 == textField){
        self.delBtn2.hidden = (self.PasswordText2.text.length == 0);

    }
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if(self.phoneText == textField){
        self.phoneView.backgroundColor = UIColor.whiteColor;
        self.PasswordView.backgroundColor = KcolorHex(@"#F7F7F7");
        self.PasswordView2.backgroundColor = KcolorHex(@"#F7F7F7");
        [DTKTool YJBK:kwt(5) andView:self.phoneView andWidth:1 andColor:KcolorHex(@"#2A79FC")];
        [DTKTool YJBK:kwt(5) andView:self.PasswordView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
        [DTKTool YJBK:kwt(5) andView:self.PasswordView2 andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    }
    if(self.PasswordText == textField){
        self.phoneView.backgroundColor = KcolorHex(@"#F7F7F7");
        self.PasswordView.backgroundColor = UIColor.whiteColor;
        self.PasswordView2.backgroundColor = KcolorHex(@"#F7F7F7");
        [DTKTool YJBK:kwt(5) andView:self.phoneView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
        [DTKTool YJBK:kwt(5) andView:self.PasswordView andWidth:1 andColor:KcolorHex(@"#2A79FC")];
        [DTKTool YJBK:kwt(5) andView:self.PasswordView2 andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    }
    if(self.PasswordText2 == textField){
        self.phoneView.backgroundColor = KcolorHex(@"#F7F7F7");
        self.PasswordView.backgroundColor = KcolorHex(@"#F7F7F7");
        self.PasswordView2.backgroundColor = UIColor.whiteColor;
        [DTKTool YJBK:kwt(5) andView:self.phoneView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
        [DTKTool YJBK:kwt(5) andView:self.PasswordView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
        [DTKTool YJBK:kwt(5) andView:self.PasswordView2 andWidth:1 andColor:KcolorHex(@"#2A79FC")];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    self.phoneView.backgroundColor = KcolorHex(@"#F7F7F7");
    self.PasswordView.backgroundColor = KcolorHex(@"#F7F7F7");
    self.PasswordView2.backgroundColor = KcolorHex(@"#F7F7F7");
    [DTKTool YJBK:kwt(5) andView:self.phoneView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    [DTKTool YJBK:kwt(5) andView:self.PasswordView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    [DTKTool YJBK:kwt(5) andView:self.PasswordView2 andWidth:1 andColor:KcolorHex(@"#E0E0E0")];

}
- (void)delClick{
    if(self.phoneText.secureTextEntry){
        [self.delBtn setImage:[UIImage imageNamed:@"look"] forState:UIControlStateNormal];
    } else {
        [self.delBtn setImage:[UIImage imageNamed:@"no_look"] forState:UIControlStateNormal];
    }
    self.phoneText.secureTextEntry = !self.phoneText.secureTextEntry;
    
}

- (void)delClick1{
    if(self.PasswordText.secureTextEntry){
        [self.delBtn1 setImage:[UIImage imageNamed:@"look"] forState:UIControlStateNormal];
    } else {
        [self.delBtn1 setImage:[UIImage imageNamed:@"no_look"] forState:UIControlStateNormal];
    }
    self.PasswordText.secureTextEntry = !self.PasswordText.secureTextEntry;
    
}

- (void)delClick2{
    if(self.PasswordText2.secureTextEntry){
        [self.delBtn2 setImage:[UIImage imageNamed:@"look"] forState:UIControlStateNormal];
    } else {
        [self.delBtn2 setImage:[UIImage imageNamed:@"no_look"] forState:UIControlStateNormal];
    }
    self.PasswordText2.secureTextEntry = !self.PasswordText2.secureTextEntry;
    
}
- (void)OKClick{
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[a-zA-Z0-9]+$"];
    if (self.phoneText.text.length < 1) {
        [self.view showWarning: @"请正确输入原密码  "];
    }else if ([self.PasswordText.text isEqualToString:@""]){
        [self.view showWarning:@"请输入新密码"];
    } else if(self.PasswordText.text.length < 6 || self.PasswordText.text.length > 20){
        [self.view showWarning:@"请设置新密码（6-20位数字、字母组成）"];
    } else if(![regexTest evaluateWithObject:self.PasswordText.text ]){
        [self.view showWarning:@"请设置新密码（6-20位数字、字母组成）"];
    } else if ([self.PasswordText2.text isEqualToString:@""]){
        [self.view showWarning:@"请再次输入新密码"];
    }else if (![self.PasswordText.text isEqualToString:self.PasswordText2.text]){
        [self.view showWarning:@"两次密码不一致"];
    }else {
        [self verifyCode];
    }
}
-(void)verifyCode{
    [self.view showBusyHUD];
    [SCNetManger PostAppAccountUpdatePasswordWithDict:@{@"userName":[UserInfoModel shared].username,
                                                        @"confirmPassword":self.PasswordText.text,
                                                        @"newPassword":self.PasswordText2.text,
                                                        @"oldPassword":self.phoneText.text,
                                               } andCompetionHandlerBlock:^(HTTPDicBase *model) {
        [self.view hideBusyHUD];
        if (model == nil) {
            [self.view showWarning:errorMsg];
        } else if(model.code == 200){
            [self.view showWarning:model.message];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UserInfoModel deleteAccount];
            });

        }else{
            [self.view showWarning:model.message];
        }
    }];
}


@end
